home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / prio / _eb_tree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  14.3 KB  |  715 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _eb_tree.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. #include <LEDA/impl/eb_tree.h>
  17.  
  18. // Stefan  Naeher ( 1989 )
  19. // Michael Wenzel ( 1990 ) 
  20.  
  21.  
  22.  
  23. // ----------------------------------------------------------
  24. //
  25. // Konstruktor & Destruktor
  26. //
  27.  
  28. l_stratified::l_stratified(stratified_ptr b, int x)
  29. {
  30.   int t = b->min2();
  31.  
  32.   if ( x == b->min() )
  33.   {
  34.     ma = t;
  35.     mi = b->max();
  36.   }
  37.  
  38.   if ( x == t )
  39.   {
  40.     ma = b->min();
  41.     mi = b->max();
  42.   }
  43.  
  44.   if ( x == b->max() )
  45.   {
  46.     ma = b->min();
  47.     mi = t;
  48.   }
  49.  
  50. }
  51.  
  52.  
  53. stratified::stratified(int i)  
  54.  
  55.   mi = ma = -1;
  56.   sz = 0; 
  57.   k = i;
  58.   top = 0;
  59.   bot = 0;
  60.  
  61. }
  62.  
  63. stratified::stratified(l_stratified_ptr l, int i)
  64.  
  65.   mi = l->min();
  66.   ma = l->max();
  67.   sz = 2;
  68.   k = i;
  69.   top = 0;
  70.   bot = 0;
  71.  
  72. }
  73.  
  74.  
  75. stratified::~stratified()  
  76.                                          // deallocate memory of sub-structures
  77.   if ((k>1) && (sz>2))
  78.   {                                      // delete stratified trees of bot
  79.     bot->clear(k);
  80.  
  81.     if ( k <= 8 )
  82.       delete bot->l;
  83.     else
  84.       delete bot->d;
  85.     delete bot;
  86.  
  87.     if ( top->size() <= 2 )
  88.       delete top;
  89.     else
  90.       delete (stratified_ptr)top;
  91.  
  92.     top = 0;
  93.     bot = 0;
  94.  
  95.   }
  96.  
  97. }
  98.  
  99. // ----------------------------------------------------------
  100. //
  101. // min2                  
  102. //
  103. // liefert 2. kleinstes Element, falls sz > 2
  104. //
  105. // -1 , sonst
  106. //
  107. // ----------------------------------------------------------
  108.  
  109. int stratified::min2()
  110.  
  111. {
  112.   if ( sz < 3 )
  113.     return -1;
  114.  
  115.   int z = top->min();
  116.   l_stratified_ptr l_bot_ptr = l_stratified_ptr(bot->lookup(z,k));
  117.  
  118.   return (mal_pot_2(z,down(k))) | (l_bot_ptr->min());
  119.  
  120. }
  121.  
  122.  
  123. // ----------------------------------------------------------
  124. //
  125. // max2                  
  126. //
  127. // liefert 2. groesstes Element, falls sz > 2
  128. //
  129. // -1 , sonst
  130. //
  131. // ----------------------------------------------------------
  132.  
  133. int stratified::max2()
  134.  
  135. {
  136.   if ( sz < 3 )
  137.     return -1;
  138.  
  139.   int z = top->max();
  140.   l_stratified_ptr l_bot_ptr = l_stratified_ptr(bot->lookup(z,k));
  141.  
  142.   return (mal_pot_2(z,down(k))) | (l_bot_ptr->max());
  143.  
  144. }
  145.  
  146.  
  147. // ----------------------------------------------------------
  148. //
  149. // succ                  
  150. //
  151. // liefert Nachfolger von x im Baum auf dieser Rekursionsstufe
  152. //                    falls existiert
  153. // -1 , sonst
  154. //
  155. // ----------------------------------------------------------
  156.  
  157. int stratified::succ(int x) 
  158.  
  159. {
  160.   if ( x >= ma ) return -1;
  161.   if ( x <  mi ) return mi;
  162.   if ( sz == 2 ) return ma;               // mi < x < ma
  163.  
  164.                                           // sz >= 2 &&  k>1
  165.   int x1 = high_bits(x); 
  166.   int x2 = low_bits(x); 
  167.  
  168.   GenPtr p = bot->lookup(x1,k);
  169.   l_stratified_ptr l_bot_ptr = l_stratified_ptr(p);
  170.  
  171.   if ( (l_bot_ptr) && (l_bot_ptr->max()>x2) )
  172.   { 
  173.     if ( l_bot_ptr->size() <= 2 )             // l_stratified Struktur
  174.         return ( mal_pot_2(x1,down(k))) | ( l_bot_ptr->succ(x2) ) ;
  175.     else
  176.     {
  177.       stratified_ptr bot_ptr = stratified_ptr(p) ;
  178.       return (mal_pot_2(x1,down(k))) | (bot_ptr->succ(x2)) ;
  179.     }
  180.   }
  181.   else                                   // succ nicht in bot-Unterstruktur
  182.   {
  183.     int z;
  184.     
  185.     if ( top->size() <= 2 )
  186.       z = top->succ(x1); 
  187.     else
  188.       z = ((stratified_ptr)top)->succ(x1); 
  189.  
  190.     if ( z == -1 ) 
  191.       return ma;                         // x unmittelbar vor Maximum
  192.  
  193.     l_stratified_ptr l_bot_ptr = l_stratified_ptr(bot->lookup(z,k));
  194.  
  195.     return (mal_pot_2(z,down(k))) | (l_bot_ptr->min()) ;
  196.   }
  197.  
  198. }
  199.  
  200. // ----------------------------------------------------------
  201. //
  202. // pred                  
  203. //
  204. // liefert Vorgaenger von x im Baum auf dieser Rekursionsstufe
  205. //                    falls existiert
  206. // -1 , sonst
  207. //
  208. // ----------------------------------------------------------
  209.  
  210. int stratified::pred(int x) 
  211.  
  212. {
  213.   if ( x >  ma ) return ma;
  214.   if ( x <= mi ) return -1;
  215.   if ( sz == 2 ) return mi;               // mi < x < ma
  216.  
  217.                       // sz > 2 && k > 1
  218.   int x1 = high_bits(x); 
  219.   int x2 = low_bits(x); 
  220.  
  221.   GenPtr p = bot->lookup(x1,k);
  222.   l_stratified_ptr l_bot_ptr = l_stratified_ptr(p);
  223.  
  224.   if ( (l_bot_ptr) && (l_bot_ptr->min()<x2) ) 
  225.   {
  226.     if ( l_bot_ptr->size() <= 2 )             // l_stratified Struktur
  227.         return ( mal_pot_2(x1,down(k))) | ( l_bot_ptr->pred(x2) ) ;
  228.     else
  229.     {
  230.       stratified_ptr bot_ptr = stratified_ptr(p) ;
  231.       return (mal_pot_2(x1,down(k))) | (bot_ptr->pred(x2)) ;
  232.     }
  233.   }
  234.  
  235.   else                                        // pred nicht in top-Unterstruktur
  236.     
  237.   {
  238.     int z;
  239.  
  240.     if ( top->size() <= 2 )
  241.       z = top->pred(x1); 
  242.     else
  243.       z = ((stratified_ptr)top)->pred(x1); 
  244.  
  245.     if ( z == -1 ) 
  246.       return mi;                              // x unmittelbar vor Maximum
  247.  
  248.     l_stratified_ptr l_bot_ptr = l_stratified_ptr(bot->lookup(z,k));
  249.  
  250.     return (mal_pot_2(z,down(k))) | (l_bot_ptr->max() ) ;
  251.   }
  252.  
  253.  
  254. // --------------------------------------------------------------- 
  255. //
  256. // member   
  257. //
  258. // liefert 1, falls Element in der Struktur
  259. //
  260. //         0, sonst
  261. //
  262. // ----------------------------------------------------------
  263.  
  264. int stratified::member(int x)
  265.   if (( x == ma )||( x == mi ))
  266.     return 1;
  267.  
  268.   if (( x < mi )||( x > ma ))
  269.     return 0;
  270.  
  271.   if ( sz == 2 )
  272.     return 0;
  273.  
  274.                      // sz > 2 && k > 1
  275.   int x1 = high_bits(x); 
  276.   int x2 = low_bits(x); 
  277.  
  278.   GenPtr p = bot->lookup(x1,k);
  279.   l_stratified_ptr l_bot_ptr = l_stratified_ptr(p);
  280.  
  281.   if (l_bot_ptr)
  282.  
  283.     if ( l_bot_ptr->size() <= 2 )    // l_stratified Struktur
  284.       return l_bot_ptr->member(x2);
  285.  
  286.     else
  287.     {
  288.       stratified_ptr bot_ptr = stratified_ptr(p);
  289.       return bot_ptr->member(x2);                  
  290.     }
  291.  
  292.   else
  293.     return 0;                
  294.  
  295. }
  296.  
  297. // ----------------------------------------------------------
  298. //
  299. // insert           
  300. //
  301. // fuegt ein Element x rekursiv in den Baum ein
  302. //
  303. // liefert 1, falls es eingefuegt wurde
  304. //
  305. //         0, falls es schon Element dewr Struktur war
  306. //
  307. // ----------------------------------------------------------
  308.  
  309. int stratified::insert(int x)
  310.   int inserted = 0;
  311.  
  312.   if ( ( x == mi ) || ( x == ma ) )      // Element in Structure
  313.     return 0;
  314.  
  315.   switch (sz)
  316.   {
  317.  
  318.     case 0: 
  319.          {
  320.            mi = ma = x;
  321.  
  322.            inserted = 1;
  323.                break;
  324.              }
  325.  
  326.     case 1:
  327.          {                           // incremental construction iff sz > 2
  328.                if ( x > mi ) 
  329.              ma = x;
  330.            else
  331.              mi = x;
  332.  
  333.            inserted = 1;
  334.                break;
  335.              }
  336.  
  337.     default: {                           // => k>1
  338.  
  339.            if ( x > ma )
  340.            { 
  341.          int t = ma;
  342.          ma = x;
  343.          x = t;
  344.                }
  345.          
  346.                if ( x < mi )
  347.            { 
  348.          int t = mi;
  349.          mi = x;
  350.          x = t;
  351.                }
  352.  
  353.                int x1 = high_bits(x);
  354.                int x2 = low_bits(x);
  355.  
  356.                                          // for incremental construction
  357.  
  358.                if ( top == 0 )           // allocate new structures
  359.                { 
  360.              top = new l_stratified(); 
  361.                  bot = new b_dict(k);
  362.                }          
  363.  
  364.                GenPtr& p = bot->lookup(x1,k);
  365.                l_stratified_ptr l_bot_ptr = l_stratified_ptr(p);
  366.  
  367.            if ( l_bot_ptr )
  368.                {
  369.                  if ( l_bot_ptr->size() == 1 )
  370.            inserted = l_bot_ptr->insert(x2);
  371.          else
  372.                  {
  373.            stratified_ptr bot_ptr;
  374.  
  375.            if ( l_bot_ptr->size() == 2 ) 
  376.                    {
  377.              if ( !l_bot_ptr->member(x2) ) 
  378.                      {
  379.                bot_ptr = new stratified(l_bot_ptr,down(k));
  380.                        inserted = bot_ptr->insert(x2);
  381.  
  382.                p = (GenPtr)bot_ptr;
  383.  
  384.                delete l_bot_ptr;
  385.                      }
  386.                    }
  387.            else       // l_bot_ptr was a bot_ptr
  388.                    {
  389.                      bot_ptr = stratified_ptr(p) ;
  390.                      inserted = bot_ptr->insert(x2);
  391.                    }
  392.                  }
  393.  
  394.                }
  395.                else                // no top entry 
  396.            {
  397.                  l_bot_ptr = new l_stratified(x2);
  398.  
  399.          if ( top->size() <= 1 )
  400.            top->insert(x1);
  401.                  else
  402.                  { 
  403.            stratified_ptr ntop;
  404.  
  405.            if ( top->size() == 2 )
  406.            { 
  407.              ntop = new stratified(top,up(k));
  408.              delete top;
  409.              top = (l_stratified_ptr)ntop;
  410.                    }
  411.            else
  412.              ntop = (stratified_ptr)top;
  413.  
  414.                ntop->insert(x1);
  415.                  }
  416.  
  417.              GenPtr help = GenPtr(l_bot_ptr);
  418.              bot->insert(x1,help,k);
  419.          inserted = 1;
  420.                }
  421.                break;
  422.              }                           // default case
  423.  
  424.   }                                      // switch
  425.  
  426.   if ( inserted )
  427.     sz++;
  428.  
  429.   return inserted;
  430.  
  431. }
  432.  
  433. // ----------------------------------------------------------
  434. //
  435. // del              
  436. //
  437. // streicht ein Element x rekursiv aus dem Baum 
  438. //
  439. // liefert 1, falls es gestrichen wurde
  440. //
  441. //         0, falls es nicht Element der Struktur war
  442. //
  443. // ----------------------------------------------------------
  444.  
  445. int stratified::del(int x)
  446.  
  447.  
  448.   int deleted = 0;
  449.  
  450.   switch (sz)
  451.   {
  452.  
  453.     case 0 : break;
  454.  
  455.     case 1 :
  456.          { 
  457.                if ( x == mi )
  458.            { 
  459.              mi = ma = -1;
  460.          deleted = 1;
  461.                }
  462.            break;
  463.              }
  464.  
  465.  
  466.  
  467.     case 2 :
  468.              {
  469.                if ( x == mi ) 
  470.                {
  471.              mi = ma;
  472.              deleted = 1;
  473.            }
  474.  
  475.                if ( x == ma )
  476.                {
  477.              ma = mi;
  478.              deleted = 1;
  479.            }
  480.  
  481.            break;
  482.              }
  483.  
  484.     case 3 :       
  485.                                    /* delete incremental construction */
  486.              {           
  487.                int t = min2();     /* third element */
  488.  
  489.                if ( x == mi )      /* new minimum   */
  490.                {
  491.               mi = t;
  492.           deleted = 1;
  493.                }
  494.  
  495.                if ( x == ma )      /* new maximum   */
  496.                {
  497.               ma = t;
  498.           deleted = 1;
  499.                }
  500.  
  501.            if ( x == t )
  502.          deleted = 1;
  503.  
  504.                if ( deleted )
  505.                            /* delete stratified trees of bot  */
  506.                {
  507.                  bot->clear(k);
  508.          if ( k <= 8 )
  509.                    delete bot->l;
  510.                  else
  511.                    delete bot->d;
  512.                  delete bot;
  513.                  bot = 0;
  514.  
  515.                  delete top;
  516.                  top = 0;
  517.                }                   /* incremental construction deleted */
  518.  
  519.            break;
  520.              }         
  521.  
  522.     default :
  523.              {
  524.            l_stratified_ptr l_bot_ptr;
  525.            stratified_ptr bot_ptr;
  526.  
  527.                if ( x == ma )                 /* delete maximum */
  528.                {
  529.          int z = max2();
  530.          ma = z;
  531.              x = z;
  532.                }
  533.  
  534.                if ( x == mi )                /* delete minimum */
  535.                {
  536.          int z = min2();
  537.          mi = z;
  538.              x = z;
  539.                }
  540.  
  541.                int x1 = high_bits(x);
  542.                int x2 = low_bits(x);
  543.  
  544.                GenPtr& p = bot->lookup(x1,k);
  545.  
  546.            if (!p)
  547.          return 0;                  /* element not in structure */
  548.  
  549.                l_bot_ptr = l_stratified_ptr(p) ;
  550.  
  551.                if ( l_bot_ptr->size() <= 2 )
  552.            {
  553.                  deleted = l_bot_ptr->del(x2);
  554.  
  555.                  if ( l_bot_ptr->size() == 0 )  /* delete structure */
  556.              {
  557.                    delete l_bot_ptr;
  558.   
  559.            if ( top->size() <= 2 )
  560.              top->del(x1);
  561.                    else
  562.            {
  563.              stratified_ptr ntop = (stratified_ptr)top;
  564.              if ( ntop->sz == 3 )
  565.              { 
  566.                top = new l_stratified(ntop,x1);
  567.                delete ntop;
  568.                      }
  569.              else
  570.                        ntop->del(x1);
  571.                    } 
  572.  
  573.                    bot->del(x1,k);
  574.                  }
  575.                }
  576.  
  577.                else                                  /* stratified structure */
  578.                {
  579.                  bot_ptr = stratified_ptr(p) ;
  580.  
  581.                  if ( bot_ptr->sz == 3 )              /* change into l_stratified */
  582.                  {
  583.            if ( bot_ptr->member(x2) )
  584.            {
  585.              l_bot_ptr = new l_stratified(bot_ptr,x2);
  586.              deleted = 1;
  587.  
  588.                      p = (GenPtr)l_bot_ptr;
  589.  
  590.                      delete bot_ptr;
  591.  
  592.                    }
  593.                  }
  594.                  else                               /* size >= 4 */
  595.                    deleted = bot_ptr->del(x2);
  596.                }
  597.  
  598.            break;
  599.              }
  600.  
  601.   }
  602.  
  603.   if ( deleted ) 
  604.     sz--;
  605.  
  606.   return deleted;
  607. }
  608.  
  609. // ----------------------------------------------------------
  610. //
  611. // print            
  612. //
  613. // druckt alle Elemente des Baumes aus            
  614. //
  615. // ----------------------------------------------------------
  616.  
  617. void stratified::print()
  618.   int i = -1;
  619.   while ((i=succ(i))>=0)
  620.     cout << i << " ";
  621. }
  622.  
  623. // -------------------------------------------------------
  624. // clear
  625. //
  626. // loescht alle Elemente, zerstoert Unterbaeume und 
  627. // setzt Dictionary auf Leerzustand
  628. //
  629. // ----------------------------------------------------------
  630.  
  631. b_dict::b_dict(int k) 
  632. { if ( k <= 8 ) 
  633.    { l = new GenPtr[pot_2(up(k))];
  634.      for (int i = 0; i < pot_2(up(k)); i++) l[i] = 0;
  635.     }
  636.   else 
  637.    d = new dp_hash;
  638. }
  639.  
  640. void b_dict::clear(int k)
  641.   if ( k <= 8 )
  642.   {
  643.     for (int i = 0; i < pot_2(up(k)); i++)
  644.     { 
  645.       l_stratified_ptr l_bot_ptr=l_stratified_ptr(l[i]);
  646.       if  (l_bot_ptr)
  647.         if (l_bot_ptr->size()<=2) 
  648.           delete l_bot_ptr;
  649.         else
  650.         { 
  651.           stratified_ptr bot_ptr=stratified_ptr(l[i]);
  652.           delete bot_ptr;
  653.         }
  654.     }
  655.  
  656.     return;
  657.   }
  658.       
  659.                                  // b_dict was a hashing structure
  660.  
  661.  
  662.   stp s = new subtable[d->n];
  663.   stp pos = s;
  664.   int i=0;
  665.  
  666.   while (i<d->sM)                        // Headertables loeschen
  667.   {
  668.     if (d->htablep[i])
  669.     {
  670.       d->htablep[i]->give_elements(pos,d->anf,d->ende);
  671.       delete d->htablep[i];                         
  672.     }
  673.     i++;
  674.   }
  675.  
  676.   for (i=0 ; i<d->n ; i++)
  677.   { 
  678.     l_stratified_ptr l_bot_ptr=l_stratified_ptr(s[i].info());
  679.     if  (l_bot_ptr)
  680.       if (l_bot_ptr->size()<=2) 
  681.         delete l_bot_ptr;
  682.       else
  683.       { 
  684.         stratified_ptr bot_ptr=stratified_ptr(s[i].info());
  685.         delete bot_ptr;
  686.       }
  687.   }
  688.  
  689.   free ((char*)d->htablep) ;             // Speicher freigeben
  690.  
  691.   if (d->anf)
  692.     delete d->anf;
  693.  
  694.   delete s;
  695.  
  696.   d->n = 0;                              // Leerinitialisierung
  697.   d->M=4;
  698.   d->sM=13;
  699.   d->k=random(1,maxprim-1);
  700.   d->bed=-17;
  701.   d->htablep=(htp*) calloc(d->sM,sizeof(htp));
  702.   d->anf = d->wo = d->ende = 0;
  703.  
  704. }
  705.